package br.edu.ufcg.dsc.psoo.mymoney.manager;

import org.mentawai.action.LogoutAction;
import org.mentawai.core.ApplicationManager;
import org.mentawai.core.Context;
import org.mentawai.core.Redirect;
import org.mentawai.filter.AuthenticationFilter;
import org.mentawai.filter.AutoWiringFilter;
import org.mentawai.filter.ExceptionFilter;
import org.mentawai.filter.FileUploadFilter;
import org.mentawai.filter.InjectionFilter;
import org.mentawai.filter.IoCFilter;
import org.mentawai.filter.RedirectAfterLoginFilter;
import org.mentawai.filter.ValidatorFilter;

import br.edu.ufcg.dsc.psoo.mymoney.model.hibernate.HibernateAccountDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.hibernate.HibernateGroupDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.hibernate.HibernateMovementDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.hibernate.HibernateTypeDAO;
import br.edu.ufcg.dsc.psoo.mymoney.model.hibernate.HibernateUserDAO;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.AccountAction;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.GroupAction;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.LoginAction;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.MovementAction;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.TypeAction;
import br.edu.ufcg.dsc.psoo.mymoney.view.action.UserAction;


/**
 * ApplicationManager do <b>Mentawai</b> <br>
 * Configura��o do framework utilizado na camada de vis�o
 * 
 * @author leonardo
 * @version 1.0
 */

public class AppManager extends ApplicationManager {


	@Override
	public void init(Context application) {

		// //////////////////////////////////////////
		// TURN ON/OFF DEBUG MODE
		// //////////////////////////////////////////
		setDebugMode(false);

		// /////////////////////////////////////////////////
		// TURN ON/OFF APP MANAGER AUTO-REDEPLOY FEATURE
		// OBS: Requires http://www.javarebel.com to work
		// /////////////////////////////////////////////////
		setReloadable(true);

	}

	@Override
	public void loadActions() {

		// ///////////////////////////////////////////
		// GLOBAL FILTERS
		// ///////////////////////////////////////////
		
		filter(new FileUploadFilter());
		
		filter(new ExceptionFilter());
		on(EXCEPTION, fwd("/jsp/error.jsp"));

		filter(new IoCFilter());

		// //////////////////////////////////////////////////////
		// INVERSION OF CONTROL: SET UP YOUR REPOSITORIES OR
		// ANY OTHER OBJECT IMPLEMENTATION YOU WANT TO MAKE
		// AVAILABLE THROUGH IOC (INVERSION OF CONTROL)
		// //////////////////////////////////////////////////////
		ioc("userDAO", HibernateUserDAO.class);
		ioc("movementDAO", HibernateMovementDAO.class);
		ioc("groupDAO", HibernateGroupDAO.class);
		ioc("accountDAO", HibernateAccountDAO.class);
		ioc("typeDAO", HibernateTypeDAO.class);

		filter(new AutoWiringFilter());

		// ////////////////////////////////////////////////////////
		// AUTHENTICATION: ALL ACTIONS THAT DO NOT IMPLEMENT
		// THE AuthenticationFree INTERFACE WILL REQUIRE
		// AUTHENTICATION
		// ////////////////////////////////////////////////////////
		filter(new AuthenticationFilter());
		on(LOGIN, redir("/jsp/loginjsp"));

		filter(new ValidatorFilter());

		filter(new InjectionFilter());

		// // ACTIONS

		action("/User", UserAction.class, "add").on(ERROR,
				fwd("/jsp/registration.jsp"))
				.on(CREATED, fwd("/Movement.showAll.mtw"));

		action("/User", UserAction.class, "edit")
				.on(SHOW,fwd("/jsp/editUser.jsp"))
				.on(ERROR, fwd("/jsp/editUser.jsp"))
				.on(UPDATED, fwd("/Movement.showAll.mtw"));

		action("/Login", LoginAction.class).filter(
				new RedirectAfterLoginFilter())
				.on(REDIR, new Redirect())
				.on(ERROR, fwd("/jsp/login.jsp"))
				.on(SUCCESS,redir("/Movement.showAll.mtw"));

		action("/Logout", LogoutAction.class).on(SUCCESS,
				redir("/jsp/login.jsp"));

		//Type CRUD
		
		action("/Type", TypeAction.class, "add")
		.on(ERROR, fwd("/jsp/type.jsp")).on(SHOW, fwd("/jsp/type.jsp"))
		.on(CREATED, fwd("/jsp/type.jsp"));
		
		

		action("/Type", TypeAction.class, "delete").on(ERROR,
				fwd("/jsp/type.jsp")).on(SHOW, fwd("/jsp/type.jsp")).on(
						REMOVED, fwd("/jsp/type.jsp"));

		action("/Type", TypeAction.class, "update").on(ERROR,
				fwd("/jsp/typeEdit.jsp")).on(SHOW, fwd("/jsp/typeEdit.jsp")).on(
						UPDATED, fwd("/jsp/typeEdit.jsp"));
		//Account CRUD
		
		action("/Account", AccountAction.class, "add").on(ERROR,
				fwd("/jsp/account.jsp")).on(SHOW, fwd("/jsp/account.jsp")).on(
				CREATED, fwd("/jsp/account.jsp"));

		action("/Account", AccountAction.class, "delete").on(ERROR,
				fwd("/jsp/account.jsp")).on(SHOW, fwd("/jsp/account.jsp")).on(
				REMOVED, fwd("/jsp/account.jsp"));

		action("/Account", AccountAction.class, "update")
		.on(ERROR,fwd("/jsp/accountEdit.jsp"))
		.on(SHOW, fwd("/jsp/accountEdit.jsp"))
		.on(UPDATED, fwd("/jsp/accountEdit.jsp"));
		
		//Group CRUD
		
		action("/Group", GroupAction.class, "add")
		.on(ERROR, fwd("/jsp/group.jsp"))
		.on(SHOW, fwd("/jsp/group.jsp"))
		.on(CREATED, fwd("/jsp/group.jsp"));

		action("/Group", GroupAction.class, "delete")
		.on(ERROR,fwd("/jsp/group.jsp"))
		.on(SHOW, fwd("/jsp/group.jsp"))
		.on(REMOVED, fwd("/jsp/group.jsp"));
		
		action("/Group", GroupAction.class, "update")
		.on(ERROR,fwd("/jsp/groupEdit.jsp"))
		.on(SHOW, fwd("/jsp/groupEdit.jsp"))
		.on(UPDATED, fwd("/jsp/groupEdit.jsp"));
		
		//Movement CRUD
		
		 action("/Movement", MovementAction.class, "add")
		 .on(ERROR, fwd("/jsp/error.jsp"))
		 .on(SHOW, fwd("/jsp/movement.jsp"))
		 .on(CREATED, fwd("/jsp/movement.jsp"));
				
		 action("/Movement", MovementAction.class, "delete")
		 .on(ERROR, fwd("/jsp/movement.jsp"))
		 .on(SHOW, fwd("/Movement.add.mtw"))
		 .on(CREATED, fwd("/jsp/movement.jsp"));
		 
		 action("/Movement", MovementAction.class, "showAll")
		 .on(ERROR, fwd("/jsp/error.jsp"))
		 .on(SHOW, fwd("/jsp/index.jsp"))
		 .on(CREATED, fwd("/jsp/movement.jsp"));
		 
		 action("/Movement", MovementAction.class, "search")
		 .on(ERROR, fwd("/jsp/error.jsp"))
		 .on(SHOW, fwd("/jsp/search.jsp"));
		 
		 action("/Movement", MovementAction.class, "upload")
		 .on(ERROR, fwd("/jsp/upload.jsp"))
		 .on(SHOW, fwd("/jsp/upload.jsp"));
		 
		 action("/Movement", MovementAction.class, "update")
		 .on(ERROR,fwd("/jsp/error.jsp"))
		 .on(SHOW, fwd("/jsp/movementEdit.jsp"));

	}

	@Override
	public void loadBeans() {

	}
}